/** 
 *
 * @author ilya portnyagin iportnyagin@gmail.com
 */

package ru.portnyagin.helpdeskru.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedProperty;
import javax.faces.model.SelectItem;
import ru.portnyagin.helpdeskru.model.Organization;
import ru.portnyagin.helpdeskru.model.Priority;
import ru.portnyagin.helpdeskru.model.Request;
import ru.portnyagin.helpdeskru.model.RoleEnum;
import ru.portnyagin.helpdeskru.model.ServiceObject;
import ru.portnyagin.helpdeskru.model.StateRequest;
import ru.portnyagin.helpdeskru.model.UserHD;
import ru.portnyagin.helpdeskru.util.JsfUtil;


public class SearchController implements Serializable {

    @EJB
    private ru.portnyagin.helpdeskru.service.RequestService requestService;
    
    @EJB
    private ru.portnyagin.helpdeskru.service.ServiceObjectService serviceObjectService;
    
    @EJB
    private ru.portnyagin.helpdeskru.service.OrganizationService organizationService;
    
    @EJB
    private ru.portnyagin.helpdeskru.service.UserHDService userHDService;
    
    @ManagedProperty("#{loginController}")
    private LoginController loginController;
    
    @ManagedProperty("#{currentRequestBean}")
    private CurrentRequestBean currentRequestBean;
    
    private List<Request> listRequest = null;
    
    protected UserHD curUser;
    
    private List<ServiceObject> listServiceObject;
    private List<Organization> listOrganization;
    private List<UserHD> listUsersCreatedBy;
    
    private Long id;
    protected Organization organization;
    private UserHD createdBy;
    private Date createdDateBegin;
    private Date createdDateEnd;
    private String description;
    private String text;
    private Priority priority;
    private ServiceObject serviceObject;
    private UserHD performer;
    private StateRequest stateRequest;    
    
    private Date stateDateBegin;
    private Date stateDateEnd;
    
    protected String actionString;
    
    
    public SearchController() {
    }
    
    @PostConstruct
    private void postConstruct() {
        curUser = loginController.getLogged();
        currentRequestBean.setActionString(actionString);
    }

    public SelectItem[] getSelectItemListUsersCreatedBy() {
        if(listUsersCreatedBy == null) {
            listUsersCreatedBy = userHDService.findUsersByOrganization(curUser.getOrganization());
        }
        return JsfUtil.getSelectItems(listUsersCreatedBy, true);
    }
        
    
    public SelectItem[] getSelectItemServiceObject() {
        if(listServiceObject == null) {
            listServiceObject = new ArrayList<>();
            if(curUser.getRole().getName().equals(RoleEnum.SERVICE)) {
                listServiceObject = serviceObjectService.findAll(); 
            } else {
                listServiceObject = serviceObjectService.findServiceObject(curUser); 
            }
        }
        return JsfUtil.getSelectItems(listServiceObject, true);
    }
    
    
    public SelectItem[] getSelectItemOrganization() {
        if(listOrganization == null) {
            listOrganization = new ArrayList<>();
            listOrganization = organizationService.findAll(); 
        }
        return JsfUtil.getSelectItems(listOrganization, true);
    }
    
    public List<Request> getListRequest() {
        return listRequest;
    }
    
    public void setLoginController(LoginController loginController) {
        this.loginController = loginController;
    }

    public void setCurrentRequestBean(CurrentRequestBean currentRequestBean) {
        this.currentRequestBean = currentRequestBean;
    }

    public CurrentRequestBean getCurrentRequestBean() {
        return currentRequestBean;
    }

    
    
    public UserHD getCurUser() {
        return curUser;
    }

    public void setCurUser(UserHD curUser) {
        this.curUser = curUser;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Organization getOrganization() {
        return organization;
    }

    public void setOrganization(Organization organization) {
        this.organization = organization;
    }

    public UserHD getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(UserHD createdBy) {
        this.createdBy = createdBy;
    }

    public Date getCreatedDateBegin() {
        return createdDateBegin;
    }

    public void setCreatedDateBegin(Date createdDateBegin) {
        this.createdDateBegin = createdDateBegin;
    }

    public Date getCreatedDateEnd() {
        return createdDateEnd;
    }

    public void setCreatedDateEnd(Date createdDateEnd) {
        this.createdDateEnd = createdDateEnd;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public Priority getPriority() {
        return priority;
    }

    public void setPriority(Priority priority) {
        this.priority = priority;
    }

    public ServiceObject getServiceObject() {
        return serviceObject;
    }

    public void setServiceObject(ServiceObject serviceObject) {
        this.serviceObject = serviceObject;
    }

    public UserHD getPerformer() {
        return performer;
    }

    public void setPerformer(UserHD performer) {
        this.performer = performer;
    }

    public StateRequest getStateRequest() {
        return stateRequest;
    }

    public void setStateRequest(StateRequest stateRequest) {
        this.stateRequest = stateRequest;
    }

    public Date getStateDateBegin() {
        return stateDateBegin;
    }

    public void setStateDateBegin(Date stateDateBegin) {
        this.stateDateBegin = stateDateBegin;
    }

    public Date getStateDateEnd() {
        return stateDateEnd;
    }

    public void setStateDateEnd(Date stateDateEnd) {
        this.stateDateEnd = stateDateEnd;
    }

    
    
    
    
      public void makeSearch() {
       listRequest = requestService.findByVariousCriteria(id, organization, createdBy, 
               createdDateBegin, createdDateEnd, description, text, serviceObject, 
               stateRequest, stateDateBegin, stateDateEnd, performer);
   }

      
      
      
      
      
      
      
      
      
      
      
      
}
